package com.deruan.hotcore.resource.command;

import org.columba.api.command.ICommandReference;
import org.columba.api.command.IWorkerStatusController;
import org.columba.api.gui.frame.IFrameMediator;
import org.columba.core.command.Command;
import org.columba.core.command.StatusObservableImpl;
import org.columba.mail.command.IMailFolderCommandReference;
import org.columba.mail.folder.IMailFolder;
import org.columba.mail.folder.IMailbox;
import org.columba.mail.folder.virtual.VirtualFolder;
import org.columba.mail.gui.frame.MailFrameMediator;
import org.columba.mail.gui.frame.TableViewOwner;
import org.columba.mail.gui.frame.TreeViewOwner;
import org.columba.mail.gui.table.ITableController;
import org.columba.mail.gui.table.action.ClearHeaderlistAction;

import com.deruan.hotcore.resource.gui.tree.ContactTreeNode;
import com.deruan.hotcore.resource.gui.tree.IResultTreeController;
import com.deruan.hotcore.resource.gui.tree.ResourceTreeNode;
import com.deruan.hotcore.resource.gui.tree.ResultTreeNode;

import edu.uci.ics.jung.graph.Vertex;

public class SeeSocialNodeCommand extends Command
{
	private IFrameMediator mediator;
	
	private boolean updateGui;
	
	private ResultTreeNode node;
	
	private IMailFolder relationFolder;
	
	public SeeSocialNodeCommand(IFrameMediator mediator,
			ICommandReference reference) {
		super(reference);

		this.mediator = mediator;

		// Register as listener to the SelectionManger
		// to check for selection changes
		updateGui = true;

		priority = Command.REALTIME_PRIORITY;
	}

	
	public void updateGUI() throws Exception {

		// Update only if the selection did not change
		if (updateGui) {
			IResultTreeController resultController = ((TreeViewOwner) mediator).getResultTreeController();
			//resourcetableController.addColumn(resourcetableController.createTableColumn("11111", 50));
			resultController.turnOnVertex(relationFolder);
		}

	}

	/**
	 * @see org.columba.api.command.Command#execute(Worker)
	 */
	public void execute(IWorkerStatusController worker) throws Exception 
	{
		// Register as SelectionListener to track the selection
		// of the tree
		if (!updateGui)
			return;

		String name = null;
		
		ResultCommandReference r = (ResultCommandReference) getReference();

		node = (ResultTreeNode) r.getSelectedNode();
		
		if(node instanceof ContactTreeNode || node instanceof ResourceTreeNode)
		{
			if(node instanceof ResourceTreeNode)
			{
				node = (ContactTreeNode)(node.getParent());
				name = node.getNameString();
			}else
			{
				name = ((ContactTreeNode)node).getNameString();
			}
			if(name==null||name.equals(""))
				return;
            //fetch the data of relation social net node
			try {
				//headerList = ((IMailbox)folder).getHeaderList();
				relationFolder = ((ContactTreeNode)node).getRelationSocialNode();
				
			} catch (Exception e) {
				updateGui = false;

				// Reset the selection
				((MailFrameMediator) mediator).setResultTreeSelection(null);
				//new ClearHeaderlistAction(mediator).actionPerformed(null);

				throw e;
			}

			updateGui &= !worker.cancelled();
		}
		else
		{
			return ;
		}

		
	}

}
